package com.haohe.wx.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;

import org.apache.log4j.Logger;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;


/**
 * 
 * @Title WXUtil.java
 * @Description 微信工具类
 * @author liming.zhao@haohedata.com
 * @date 2015-6-29 
 * @time 上午11:38:53
 */
public class WXUtil {
	
	private static Logger logger = Logger.getLogger(WXUtil.class);

	/**
	 * 流转换成字符串
	 * 
	 * @param inputStream
	 * @return
	 * @throws IOException
	 */
	public static synchronized String parseStream2Str(InputStream inputStream) {
		StringBuffer receiveMessage = new StringBuffer();
		Scanner scanner = new Scanner(inputStream, "UTF-8");
		while (scanner.hasNext()) {
			receiveMessage.append(scanner.next());
		}
		scanner.close();
		return receiveMessage.toString();
	}

	/**
	 * 字符串转换成xml节点
	 * 
	 * @param str
	 * @return
	 */
	public static synchronized Element parseStr2Ele(String str) {
		Document document = null;
		try {
			document = DocumentHelper.parseText(str);
		} catch (DocumentException e) {
			logger.error("微信获取信息转换成节点类型失败："+e);
		}
		return document.getRootElement();
	}
	
	
}